﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:De.LarsHildebrandt.WpfControls.WpfSimpleChart"
    xmlns:converters="clr-namespace:De.LarsHildebrandt.WpfControls.WpfSimpleChart.Converters">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/De.LarsHildebrandt.WpfControls.WpfSimpleChart;component/ResourceDictionaries/ChartToolTip.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <converters:BarChartItemWidthConverter x:Key="itemWidthConverter"/>
    <converters:StackedChartHeightConverter x:Key="itemHeightConverter"/>
    <converters:BarChartItemVisibilityRowDefinitionConverter x:Key="itemVisibilityRowDefinitionConverter"/>

    <Style TargetType="{x:Type local:StackedChart}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:StackedChart}">
                    <Border
                        Background="{Binding Path=ChartBackground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                        BorderBrush="{Binding Path=ChartBackgroundBorderColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                        BorderThickness="{Binding Path=ChartBackgroundBorderThickness, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                        CornerRadius="{Binding Path=ChartBackgroundCornerRadius, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                        >
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Label Grid.Row="0" HorizontalAlignment="Center" FontSize="12">
                                <Label.Content>
                                    <Binding Path="Caption" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}"/>
                                </Label.Content>
                            </Label>
                            <ScrollViewer 
                                Grid.Row="1" 
                                HorizontalScrollBarVisibility="{Binding Path=ChartScrollBarVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                                VerticalScrollBarVisibility="{Binding Path=ChartScrollBarVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}">
                                <ItemsControl 
                                    x:Name="itemsControl" 
                                    ItemsSource="{Binding}"
                                    Margin="5"
                                    Grid.Row="1">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="*" />
                                                    <RowDefinition>
                                                        <RowDefinition.Height>
                                                            <Binding 
                                                                Converter="{StaticResource itemVisibilityRowDefinitionConverter}" 
                                                                UpdateSourceTrigger="PropertyChanged"
                                                                Path="ChartShowCaption" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}">
                                                            </Binding>
                                                        </RowDefinition.Height>
                                                    </RowDefinition>
                                                </Grid.RowDefinitions>
                                                <ItemsControl 
                                                    ItemsSource="{Binding Path=Values}"
                                                    Margin="{Binding Path=ChartMargin, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}">
                                                    <ItemsControl.Width>
                                                        <MultiBinding Converter="{StaticResource itemWidthConverter}" UpdateSourceTrigger="PropertyChanged">
                                                            <Binding Path="ActualWidth" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}"/>
                                                            <Binding Path="Items.Count" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}"/>
                                                            <Binding Path="ChartMargin" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}"/>
                                                            <Binding Path="ShowScrollbars" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}"/>
                                                            <Binding Path="ChartItemWidth" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}"/>
                                                        </MultiBinding>
                                                    </ItemsControl.Width>
                                                    <ItemsControl.ItemsPanel>
                                                        <ItemsPanelTemplate>
                                                            <StackPanel VerticalAlignment="Bottom" Orientation="Vertical" IsItemsHost="True"></StackPanel>
                                                        </ItemsPanelTemplate>
                                                    </ItemsControl.ItemsPanel>
                                                    <ItemsControl.ItemTemplate>
                                                        <DataTemplate>
                                                            <Grid>
                                                                <Border 
                                                                    VerticalAlignment="Bottom"
                                                                    Background="{Binding Path=ChartBrush}"
                                                                    BorderThickness="{Binding Path=ChartBorderThickness, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                                                                    BorderBrush="{Binding Path=ChartBorderColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                                                                    CornerRadius="{Binding Path=ChartCornerRadius, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                                                                    >
                                                                    <Border.ToolTip>
                                                                        <ToolTip Style="{StaticResource toolTipStyle}" />
                                                                    </Border.ToolTip>
                                                                    <Border.Height>
                                                                        <MultiBinding Converter="{StaticResource itemHeightConverter}" UpdateSourceTrigger="PropertyChanged">
                                                                            <Binding Path="Value"/>
                                                                            <Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}"/>
                                                                            <Binding Path="Items" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=2}"/>
                                                                        </MultiBinding>
                                                                    </Border.Height>
                                                                </Border>
                                                            </Grid>
                                                        </DataTemplate>
                                                    </ItemsControl.ItemTemplate>
                                                </ItemsControl>
                                                <Label 
                                                Visibility="{Binding Path=ChartShowCaption, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                                                HorizontalAlignment="Center"
                                                Grid.Row="1" 
                                                Content="{Binding Path=Caption}">
                                                </Label>
                                            </Grid>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </ScrollViewer>
                            <ItemsControl
                                Grid.Row="2"
                                Margin="5"
                                Visibility="{Binding Path=ChartLegendVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}"
                                ItemsSource="{Binding Path=ChartLegend, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:StackedChart}, AncestorLevel=1}}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel IsItemsHost="True">
                                            
                                        </WrapPanel>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Rectangle Stroke="Black" StrokeThickness="0.5" Height="10" Width="20" Fill="{Binding Path=ItemBrush}"/>
                                            <Label Content="{Binding Path=Caption}"></Label>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>